const app = getApp()
Page({
    data: {
      resourceDomain: app.resourceDomain,
      canRecall: false,
      gao_on: '',
      scroll_true: '',
      qiam_type: '',
      qian_type: '',
      isSend: false
    },
    onLoad: function (options) {
        this.page_num = 1
        var that = this
        // app.request(app.apiDomain + 'index.php?c=user&a=getRouse',{
        //     page: 1,
        //     size: 20
        // },res=>{
        //     // 如果list数量没有size大,没必要加载
        //     if(res.data.data.list.length < 10){
        //         that.scroll_suo = true
        //     }
        //     console.log(res);
        //     var icon = parseInt(res.data.data.point)
        //     if(res.data.data.list.length>0){
        //         that.setData({
        //             canRecall: true,
        //             list: res.data.data.list,
        //             icon: that.countIcon(icon.toString()),
        //         })
        //     }
        // })
        that.getData()
        // 消息
        app.request(app.apiDomain + 'index.php?c=mall&a=notice',{
            type: 'rouse'
        },res=>{
            console.log(res);
            that.setData({
    			gao: res.data.data
    		});
        })
    },
    getData() {
      var that = this
      app.request(app.apiDomain + 'index.php?c=user&a=getRouse', {
        page: 1,
        size: 20
      }, res => {
        // 如果list数量没有size大,没必要加载
        if (res.data.data.list.length < 10) {
          that.scroll_suo = true
        }
        console.log(res);
        var icon = parseInt(res.data.data.point)
        if (res.data.data.list.length > 0) {
          that.setData({
            canRecall: true,
            list: res.data.data.list,
            icon: that.countIcon(icon.toString()),
          })
        }
      })
    },
    // 计算数字
    countIcon(num){
        var num1 = num.substring(num.length-1,num.length);
        var num2 = num.substring(num.length-2,num.length-1)?num.substring(num.length-2,num.length-1):0;
        var num3 = num.substring(num.length-3,num.length-2)?num.substring(num.length-3,num.length-2):0;
        return [num1,num2,num3]
    },
    gao_click(){
        this.setData({
            gao_on: 'on'
        })
    },
    gao_off: function() {
        this.setData({
            gao_on: ''
        })
    },
    // 送金币
    sendGold: function (e) {
      let id = e.currentTarget.dataset.id
      this.setData({
        qian_on: 'on',
        user_id: id
      })
    },
    // 关闭送金币
    qian_off: function () {
      this.setData({
        qian_on: '',
      })
    },
    qian_click: function (e) {
      var a = e.currentTarget.dataset.id;
      this.qian_num = a >= 0 ? a : 20;
      if (a == 'a') {
        this.qiam_type = '2'
      } else if (a == 'b') {
        this.qiam_type = '3'
      } else if (a == 'c') {
        this.qiam_type = '4'
      } else {
        this.qiam_type = '1'
      }
      console.log(a,this.qiam_type)
      this.setData({
        qian_type: 'type_' + e.currentTarget.dataset.id
      })
    },
    // 选择金币发送 
    qian_submit() {
      let that = this
      if (that.data.qian_type) {
        app.request(app.apiDomain + 'index.php?c=user&a=shang', {
          user_id: that.data.user_id,
          money: that.qian_num
        }, function (res) {
          if (res.data.status == 1) {
            wx.showToast({
              title: '金币打赏成功，去通知好友吧！',
              icon: 'none',
              duration: 1000
            })
            // console.log('发送成功')
            // that.getData()
            for (let item of that.data.list) {
              if (item.id == that.data.user_id) {
                item.shang = true
                item.money = that.qian_num
              }
            }
            that.setData({
              qian_on: '',
              list: that.data.list
            })
          } else{
            wx.showToast({
              title: res.data.info,
              icon: 'none',
              duration: 1000
            })
          }
          that.setData({
            qian_on: '',
            qian_type: ''
          })
        })
      } else {
        wx.showToast({
          title: '请选择金币金额',
          icon: 'none',
          duration: 1000
        })
      }
    },
    // 下拉加载
    scroll_get(){
        if(this.scroll_suo) {return false}
        var that = this;
        this.setData({
            scroll_true: 'on'
        })
        this.page_num++
        app.request(app.apiDomain+'index.php?c=user&a=getRouse',{
            page: that.page_num,
            size: 20
        },res=>{
            this.setData({
                scroll_true: ''
            })
            // console.log(res);
            if(res.data.data.list.length==0){
                wx.showToast({
                    title: '已经到底了',
                    icon: 'none',
                    duration: 2000
                })
                that.scroll_suo = true;
            }else {
                that.setData({
                    list: that.data.list.concat(res.data.data.list)
                })
            }
        })
    },
    onShareAppMessage: function (res) {
      var that = this
      var nickName = '';
      var user_id = 0;
      // var time = Date.parse(new Date()).toString();
      // time = time.substr(0, 10);
      try {
          nickName = wx.getStorageSync('nickname');
          user_id = wx.getStorageSync('user_id');
      } catch (e) {}
      console.log(res.target.dataset.money)
      if (res.target.id == 2) {
        return {
          path: '/pages/index/index?user_id=' + user_id + '&rouse_user_id=' + user_id,
          title: '每天多运动，不止健康，更可荣誉加身！',
          imageUrl: app.resourceDomain + 'canvas/healthy/img/badge/share_badge.jpg'
        }
      } else {
        return {
          path: '/pages/index/index?user_id=' + user_id + '&rouse_user_id=' + user_id,
          title: '我给您送了' + res.target.dataset.money + '个健康金币,快来领取吧！',
          imageUrl: app.resourceDomain + 'canvas/healthy/img/badge/share_badge.jpg'
        }
      }
    }
})